home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 1.0 for Developers / QuickTime 1.0 for Developers.iso / Programming Stuff / Sample Code / MiniEdit / Mini Edit Main.c next >
C/C++ Source or Header  |  1991-09-11  |  16KB  |  701 lines

  1. /**************************************************
  2. *
  3. * Sample Movie Edit controls
  4. *
  5. *    This program is an extension of mini player 3
  6. *    and demonstrates the editing features of the standard controller
  7. *
  8. *    Sample.h has definitions and equates
  9. *    Main.c contains the general Mac application stuff
  10. *    Movie Stuff.c has most of the interesting movie code
  11. *
  12. *    Interesting things to look at:
  13. *    Enabling editing for the standard controller
  14. *    Enabling the edit menu items
  15. *    Handling the undo, cut, copy and paste events
  16. *
  17. *     Rich Williams 7/91
  18. *    Updated to MPW/Think 5.0/DSG interfaces 9/91 Chris Thorman
  19. *
  20. ***************************************************/
  21.  
  22. /**************************************************
  23. *
  24. * General Info
  25. *
  26. ***************************************************/
  27. #include "Movies.h"
  28. #include "Mini Edit.h"
  29.  
  30. #include <Memory.h>
  31. #include <Palettes.h>
  32. #include <Fonts.h>
  33. #include <OSEvents.h>
  34. #include <ToolUtils.h>
  35. #include <Desk.h>
  36. #include <Resources.h>
  37. #include <Sound.h>
  38. #include <GestaltEqu.h>
  39.  
  40. extern    short    numOpenMovies;
  41. extern    MovieInstance    *activeMovie;        /* Pointer to the front movie */
  42.  
  43.  
  44. Boolean    exitFlag;
  45. Boolean hasWNE;
  46.  
  47. /**************************************************
  48. *
  49. *  various globals
  50. *
  51. ***************************************************/
  52.  
  53. extern    short    theModifiers;                    /* Used to keep the event modifiers */
  54. Rect        dragRect;
  55. MenuHandle    appleMenu, fileMenu, editMenu;
  56.  
  57. MovieInstance    movieList[maxMovies];        /* Array of open movies */
  58.  
  59.  
  60. /**************************************************
  61. ***************************************************
  62. *
  63. * Initialization Routines
  64. *
  65. * All of the stuff we call to get things started
  66. *
  67. ***************************************************
  68. ***************************************************/
  69.  
  70. /**************************************************
  71. *
  72. * InitMacintosh() Initialize all the managers & memory
  73. *
  74. ***************************************************/
  75. void InitMacintosh()
  76.  
  77. {
  78.     MaxApplZone();
  79.     InitGraf(&qd.thePort);
  80.     InitFonts();
  81.     FlushEvents(everyEvent, 0);
  82.     InitWindows();
  83.     InitMenus();
  84.     TEInit();
  85.     InitDialogs(0L);
  86.     InitCursor();
  87.  
  88. }
  89.  
  90. /**************************************************
  91. *
  92. * SetUpMenus()
  93. *
  94. * Reads in the menu resources and installs them
  95. *
  96. *
  97. ***************************************************/
  98. void SetUpMenus()
  99.  
  100. {
  101.     InsertMenu(appleMenu = GetMenu(appleID), 0);
  102.     InsertMenu(fileMenu = GetMenu(fileID), 0);
  103.     InsertMenu(editMenu = GetMenu(editID), 0);
  104.     DrawMenuBar();
  105.     AddResMenu(appleMenu, 'DRVR');
  106.     
  107. }
  108.  
  109. /**************************************************
  110. *
  111. * SetUpWindows()
  112. *
  113. * INits stuff for windows
  114. *
  115. ***************************************************/
  116. void SetUpWindows()
  117.  
  118. {    
  119.     /* Create the window for the picture */    
  120.     dragRect = qd.screenBits.bounds;
  121.  
  122. }
  123.  
  124.  
  125. /**************************************************
  126. ***************************************************
  127. *
  128. *    Routines for handling menus.
  129. *
  130. ***************************************************
  131. ***************************************************/
  132.  
  133. /**************************************************
  134. *
  135. *  AdjustMenus()
  136. *
  137. *    Enable or disable the items in the Edit menu if a DA window
  138. *    comes up or goes away. Our application doesn't do anything with
  139. *    the Edit menu.
  140. *
  141. ***************************************************/
  142. void AdjustMenus()
  143. {
  144.     register WindowPeek wp = (WindowPeek) FrontWindow();
  145.     short kind = wp ? wp->windowKind : 0;
  146.     Boolean DA = kind < 0;
  147.     long    controllerFlags = 0;
  148.     OSErr    theErr;
  149.     
  150.     if(DA)                    /* Enable the edit menu if it is a DA */
  151.     {
  152.         EnableItem(editMenu, undoItem);
  153.         EnableItem(editMenu, cutItem);
  154.         EnableItem(editMenu, copyItem);
  155.         EnableItem(editMenu, pasteItem);
  156.         EnableItem(editMenu, clearItem);
  157.         DisableItem(editMenu, selectAllItem);
  158.         EnableItem(fileMenu, closeItem);
  159.         DisableItem(fileMenu, saveItem);
  160.     }
  161.     else 
  162.     {
  163.         if (activeMovie)            /* Is there a movie in the front window? */
  164.         {
  165.             if ( MCGetControllerInfo(activeMovie->movieController,&controllerFlags) == 0 ) {
  166.                 enable(fileMenu, saveItem,HasMovieChanged(activeMovie->movie));
  167.                 EnableItem(fileMenu, closeItem);
  168.                 EnableItem(editMenu, selectAllItem);
  169.             }
  170.  
  171.         }
  172.         else
  173.         {
  174.             DisableItem(fileMenu, saveItem);
  175.             DisableItem(fileMenu, closeItem);
  176.             DisableItem(editMenu, selectAllItem);
  177.         }
  178.  
  179.         enable(editMenu,undoItem,controllerFlags & mcInfoUndoAvailable);
  180.         enable(editMenu,cutItem,controllerFlags & mcInfoCutAvailable);
  181.         enable(editMenu,copyItem,controllerFlags & mcInfoCopyAvailable);
  182.         enable(editMenu,pasteItem,controllerFlags & mcInfoPasteAvailable);
  183.         enable(editMenu,clearItem,controllerFlags & mcInfoClearAvailable);
  184.         
  185.         enable(fileMenu, openItem, numOpenMovies<maxMovies);
  186.         enable(fileMenu, closeItem,numOpenMovies);
  187.     }
  188.     
  189.  
  190. }
  191.  
  192. static
  193. void enable(MenuHandle menu, short item, Boolean ok)
  194. {
  195.     if (ok)
  196.         EnableItem(menu, item);
  197.     else
  198.         DisableItem(menu, item);
  199. }
  200.  
  201. /**************************************************
  202. *
  203. *  HandleMenu (mSelect)
  204. *
  205. *    Handle the menu selection. mSelect is what MenuSelect() and
  206. *    MenuKey() return: the high word is the menu ID, the low word
  207. *    is the menu item
  208. *
  209. ***************************************************/
  210. void HandleMenu (mSelect)
  211.  
  212. long    mSelect;
  213.  
  214. {
  215.     short            menuID = HiWord(mSelect);
  216.     short            menuItem = LoWord(mSelect);
  217.     Str255        name;
  218.     GrafPtr        savePort;
  219.     WindowPeek    frontWindow;
  220.     
  221.     switch (menuID)
  222.     {
  223.         case    appleID:                /* The Apple menu */
  224.             switch (menuItem)
  225.             {
  226.                 case    aboutItem:
  227.                     DoAboutBox();        /* Display the About Box */
  228.                     break;
  229.                     
  230.                 default:                /* It's a DA */
  231.                     GetPort(&savePort);
  232.                     GetItem(appleMenu, menuItem, name);
  233.                     OpenDeskAcc(name);
  234.                     SetPort(savePort);
  235.                     break;
  236.             }
  237.             break;
  238.     
  239.           case    fileID:                    /* The file menu */
  240.             switch (menuItem)
  241.               {
  242.                   case    openItem:
  243.                       DoOpen();            /* Open a movie */
  244.                     break;
  245.                                 
  246.                   case    closeItem:
  247.                     if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
  248.                           break;
  249.               
  250.                     if (frontWindow->windowKind < 0)
  251.                           CloseDeskAcc(frontWindow->windowKind);
  252.                     else
  253.                         /* It's the movie window ( the only one we've got ) */
  254.                         /* Close it & clean up the movie stuff */
  255.                         CloseEm( (WindowPtr) frontWindow);
  256.  
  257.                     break;
  258.  
  259.                 case    saveItem:        /* save the file */
  260.                     SaveTheMovie(activeMovie);
  261.                     break;
  262.  
  263.                                                             
  264.                 case    quitItem:        /* Pack up and go home */
  265.                     exitFlag = true;
  266.                     break;
  267.             }
  268.             break;
  269.                   
  270.         case    editID:
  271.             /* DA edit? */
  272.             if (!SystemEdit(menuItem-1))
  273.                 if(activeMovie)
  274.                       DoMovieEdit(activeMovie, menuItem);        /* Handle the movie edit */
  275.             break;
  276.       }
  277. }
  278.  
  279. /**************************************************
  280. *
  281. *  CloseEm(w:WindowPtr) Closes window w 
  282. *    if w is a movie window, clean up the movie and the player
  283. *
  284. ***************************************************/
  285. void CloseEm(w)
  286.  
  287. WindowPtr w;
  288.  
  289. {
  290.     MovieInstance    *theMovie;
  291.     
  292.     HideWindow(w);
  293.     theMovie = WhichMovieWindow(w);    /* Is it a movie window */
  294.     if (theMovie)    /* If its the movie window, throw out the movie */
  295.     {
  296.         CleanUpMovie(theMovie);
  297.     }
  298. }
  299.  
  300.  
  301. /**************************************************
  302. *
  303. *  CloseAllWindows() 
  304. *    Throws out all windows & movies
  305. *
  306. ***************************************************/
  307. void CloseAllWindows()
  308.  
  309. {
  310.     short    movieCount;
  311.     
  312.     /* Throw out all of the movies */
  313.     for (movieCount = 0;movieCount<maxMovies;movieCount++)
  314.         CleanUpMovie(&movieList[movieCount]);
  315.  
  316.     /* Put any other windows here */
  317. }
  318.  
  319. /**************************************************
  320. *
  321. *  DoAboutBox() Draws and displays the about box
  322. *
  323. ***************************************************/
  324. void DoAboutBox()
  325.  
  326. {
  327.     DialogPtr    theDialog;        /* Stuff for the about box */
  328.     short            itemHit, io;
  329.     Handle        tempHandle, soundHandle;
  330.     Rect        box;
  331.     
  332.     soundHandle = GetResource('snd ',meowID);
  333.     theDialog = GetNewDialog(aboutDlgID,0L,(WindowPtr)-1L);
  334.     
  335.     /* Highlight the default button */
  336.     SetPort(theDialog);
  337.     GetDItem(theDialog,1,&itemHit,&tempHandle,&box);
  338.     PenSize(3,3);
  339.     InsetRect(&box,-4,-4);
  340.     FrameRoundRect(&box,16,16);
  341.     
  342.     /* Wait until the button is pressed */
  343.     do
  344.         ModalDialog(0l,&itemHit);
  345.     while (itemHit != 1);
  346.     
  347.     /* Button pressed, now meow and go home */
  348.     io = SndPlay(0L,soundHandle,0);
  349.     ReleaseResource(soundHandle);
  350.     DisposDialog(theDialog);
  351. }
  352.  
  353. void
  354. DoDoubleClick()
  355. {
  356.     AppFile     apfile;
  357.     short         printIt,m,n;
  358.     FSSpec        fsSpec;
  359.  
  360.     CountAppFiles(&printIt,&n);
  361.     
  362.     if(n>0) {
  363.         for(m=0;m<n;m++){
  364.             GetAppFiles(m+1,&apfile);
  365.             FSMakeFSSpec(apfile.vRefNum,0L,apfile.fName,&fsSpec);
  366.             if ( apfile.fType == 'MooV' ) 
  367.                 OpenTheMovie(&fsSpec);
  368.             if ( printIt ) {
  369.  
  370.  
  371.             }
  372.             ClrAppFiles(m+1);
  373.         }
  374.     }
  375. }
  376.  
  377.  
  378.  
  379. OSErr 
  380. MissedAEParameters (AppleEvent *message)
  381. {
  382.     DescType typeCode;
  383.     Size actualSize;
  384.     OSErr err;
  385.  
  386.     err = AEGetAttributePtr(message, keyMissedKeywordAttr, typeWildCard,
  387.             &typeCode, nil, 0L, &actualSize);
  388.     if (err == errAEDescNotFound)
  389.         return(noErr);
  390.     return(err = noErr ? errAEEventNotHandled : err);
  391. }
  392.  
  393. pascal OSErr 
  394. OpenDocMessage(AppleEvent *message, AppleEvent *reply, long refcon)
  395. {
  396.     FSSpec fSpec;
  397.     AEDescList docList;
  398.     long index, itemsInList;
  399.     Size actualSize;
  400.     AEKeyword keywd;
  401.     DescType typeCode;
  402.     OSErr err;
  403.  
  404.     if ((err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList)) != noErr)
  405.         return(err);
  406.     if ((err = MissedAEParameters(message)) != noErr)
  407.         return(err);
  408.     if ((err = AECountItems(&docList, &itemsInList)) != noErr)
  409.         return(err);
  410.  
  411.     for (index = 1; index <= itemsInList; index++) {
  412.         if ((err = AEGetNthPtr(&docList, index, typeFSS, &keywd, &typeCode,
  413.                     (Ptr)&fSpec, sizeof(FSSpec), &actualSize)) != noErr)
  414.                 break;
  415.         OpenTheMovie(&fSpec);
  416.     }
  417.  
  418.     return(AEDisposeDesc(&docList));
  419. }
  420.  
  421. pascal OSErr 
  422. QuitAppMessage(AppleEvent *message, AppleEvent *reply, long refcon)
  423. {
  424.     FSSpec fSpec;
  425.     AEDescList docList;
  426.     long index, itemsInList;
  427.     Size actualSize;
  428.     AEKeyword keywd;
  429.     DescType typeCode;
  430.     OSErr err;
  431.  
  432.     if ((err = MissedAEParameters(message)) != noErr)
  433.         return(err);
  434.  
  435.     exitFlag = true;
  436.  
  437.     return(0);
  438. }
  439.  
  440. /**************************************************
  441. *
  442. * DoOpen()
  443. *
  444. *    Brings up standard file dialog
  445. *    and opens the movie if one has been selected
  446. *
  447. ***************************************************/
  448. void DoOpen(void)
  449.  
  450. {
  451.     StandardFileReply        sfr;        /* New-style SF reply */
  452.     SFTypeList    myTypeList = {'MooV'};
  453.     
  454.     StandardGetFilePreview(0, 1, myTypeList, &sfr);
  455.     if (sfr.sfGood)
  456.         OpenTheMovie(&sfr.sfFile);    
  457.  
  458. }
  459.  
  460. /**************************************************
  461. ***************************************************
  462. *
  463. *        The window routines
  464. *
  465. ****************************************************
  466. ***************************************************/
  467.  
  468. /**************************************************
  469. *
  470. * WhichMovieWindow(w) 
  471. *    Checks if window is for a movie
  472. *     and returns a pointer to it
  473. *    or 0 if it isn't a movie's window
  474. *
  475. ***************************************************/
  476. MovieInstance* WhichMovieWindow(w)
  477.  
  478. WindowPtr    w;
  479.  
  480. {
  481.     short    movieCount;
  482.     
  483.     /* Loop through looking for a match */
  484.     for (movieCount = 0;movieCount<maxMovies;movieCount++)
  485.         if (w == movieList[movieCount].window)
  486.             break;
  487.             
  488.     if (movieCount<maxMovies)    /* Did we find one? */
  489.         return(&movieList[movieCount]);
  490.     
  491.     return(0L);                    /* Didn't find one */
  492. }
  493.         
  494. /**************************************************
  495. *
  496. * IsMyWindow(w):Boolean 
  497. *    Checks if w is a valid window and mine
  498. *
  499. ***************************************************/
  500. Boolean IsMyWindow(w)
  501.  
  502. WindowPtr    w;
  503.  
  504. {
  505.     return( (w != 0L) && 
  506.              WhichMovieWindow(w)  );
  507. }
  508.  
  509.     
  510. /**************************************************
  511. ***************************************************
  512. *
  513. * Event handling stuff
  514. *
  515. **************************************************** 
  516. ***************************************************/
  517.  
  518. /**************************************************
  519. *
  520. * HandleMouseDown (theEvent)
  521. *
  522. *    Take care of mouseDown events.
  523. *
  524. ***************************************************/
  525. void HandleMouseDown(theEvent)
  526.  
  527. EventRecord    *theEvent;
  528.  
  529. {
  530.     WindowPtr    theWindow;
  531.     short            windowCode = FindWindow (theEvent->where, &theWindow);
  532.     
  533.     switch (windowCode)
  534.     {
  535.         case inSysWindow: 
  536.             SystemClick (theEvent, theWindow);
  537.             break;
  538.         
  539.           case inMenuBar:
  540.               AdjustMenus();
  541.             HandleMenu(MenuSelect(theEvent->where));
  542.             break;
  543.         
  544.           case inDrag:
  545.               if (IsMyWindow(theWindow))
  546.                   SelectWindow(theWindow);
  547.                     DragWindow(theWindow, theEvent->where, &dragRect);
  548.                 break;
  549.             
  550.           case inContent:
  551.               if (IsMyWindow(theWindow))
  552.                 {
  553.                     if (theWindow != FrontWindow())
  554.                       SelectWindow(theWindow);
  555.                     else
  556.                       MovieMouseDown(theWindow,theEvent->where,theEvent->modifiers); /* Track the mouse */
  557.                 }
  558.               break;
  559.           
  560.         case inGoAway:
  561.               if (IsMyWindow(theWindow) && TrackGoAway(theWindow, theEvent->where))
  562.                 CloseEm(theWindow);        /* Close the appropriate windows */
  563.             break;
  564.         
  565.         break;
  566.       }
  567. }
  568.  
  569.  
  570. /**************************************************
  571. *
  572. * HandleEvent()
  573. *
  574. *        The main event dispatcher. This routine should be called
  575. *        repeatedly (it  handles only one event).
  576. *
  577. ***************************************************/
  578. void HandleEvent()
  579.  
  580. {
  581.     short            ok;
  582.     EventRecord    theEvent;
  583.     WindowPtr    theWindow;
  584.     
  585.     MovieInstance    *theMovie;
  586.  
  587.     HiliteMenu(0);
  588.     
  589.     if (  hasWNE  ) {
  590.         ok = WaitNextEvent (everyEvent, &theEvent,0,0);
  591.     } else {
  592.     
  593.         SystemTask();
  594.         ok = GetNextEvent (everyEvent, &theEvent);
  595.     }
  596.     /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  597.     !
  598.     ! Don't miss this!
  599.     ! MoviesTask is called from the main event loop to 
  600.     ! give extra time to the active movie
  601.     ! this is optional since CheckMovieControllers will update
  602.     ! all of the movies.  It also doesn't work too well yet.
  603.     !
  604.     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  605.     if(theEvent.what == nullEvent)
  606.             MyMoviesTask();
  607.     
  608.     /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  609.     !
  610.     ! CheckMovieControllers is called from the main event loop to 
  611.     ! see if the event is for the player
  612.     !
  613.     !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  614.     theModifiers = theEvent.modifiers;        /* We may need these in the filter */
  615.     if (CheckMovieControllers(&theEvent))
  616.         return;
  617.  
  618.     else if (ok)
  619.       switch (theEvent.what)
  620.       {        
  621.         case mouseDown:
  622.             HandleMouseDown(&theEvent);
  623.             break;
  624.             
  625.         case keyDown: 
  626.         case autoKey:
  627.             if ((theEvent.modifiers & cmdKey) != 0)
  628.             {
  629.               AdjustMenus();
  630.               HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
  631.             }
  632.             break;
  633.             
  634.         case updateEvt:
  635.             /* To update the window, make the player replay the last frame */
  636.             theWindow = (WindowPtr) theEvent.message;    /* Get the window */
  637.  
  638.             theMovie = WhichMovieWindow(theWindow);            /* Is it a Movie window? */
  639.  
  640.             BeginUpdate(theWindow);            
  641.             if(theMovie)
  642.                 DoMovieUpdate(theMovie);
  643.             DrawControls(theWindow);
  644.             EndUpdate(theWindow);
  645.             break;
  646.             
  647.         case activateEvt:
  648.             theMovie = WhichMovieWindow((WindowPtr) theEvent.message);        /* Is it a Movie window? */
  649.             if (theMovie)
  650.             {
  651.                 if (theEvent.modifiers & activeFlag)
  652.                     DoMovieActivate(theMovie);
  653.                 else
  654.                     DoMovieDeactivate(theMovie);
  655.             }
  656.             else        /* Some other window, make it redraw */
  657.             {
  658.                 SetPort( (WindowPtr) theEvent.message );
  659.                 InvalRect(&((WindowPtr)theEvent.message)->portRect);    /* Force an update event */
  660.             }
  661.             break;
  662.             
  663.         case kHighLevelEvent:
  664.             AEProcessAppleEvent(&theEvent);
  665.             break;
  666.        }
  667. }
  668.  
  669. /**************************************************
  670. *
  671. * main()
  672. *
  673. *    This is where everything happens
  674. *
  675. ***************************************************/
  676. main()
  677.  
  678. {    
  679.     long    resp;
  680.  
  681.     InitMacintosh();        /* Initialize everything */
  682.     SetUpMenus();
  683.     SetUpWindows();
  684.     SetUpMovies();
  685.     exitFlag = false;
  686.  
  687.     hasWNE = NGetTrapAddress(0x60,ToolTrap) != NGetTrapAddress(0x9f,ToolTrap);
  688.  
  689.     if(    Gestalt(gestaltAppleEventsAttr, &resp) == 0 &&  resp != 0) {
  690.         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  691.                 (EventHandlerProcPtr)OpenDocMessage, 0, false);
  692.         AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  693.                 (EventHandlerProcPtr)QuitAppMessage, 0, false);
  694.     }
  695.     DoDoubleClick();
  696.     while (!exitFlag )
  697.         HandleEvent();
  698.     CloseAllWindows();
  699.     ExitToShell();
  700. }
  701.